package junyi.power;

import junyi.card.FunctionModifier;
import junyi.power.effects.Effect;

import java.util.ArrayList;
import java.util.List;

/**
 * User: Junyi BEI
 * Date: 12-10-2
 * Time: 下午10:20
 */
public class ActionChain {

  public final static int ACTION_CHAIN_SUCCESS = Action.ACTION_SUCCESS;
  public final static int ACTION_UNDEFINED = Action.ACTION_EXCEPTION;
  public final static int ACTION_FAILURE = Action.ACTION_FAILURE;

  private List<Action> actions;
  private FunctionModifier functionModifier;
  private int step;

  public static ActionChain getActionChain(Action a, FunctionModifier f, int s) {
    return new ActionChain(a, f, s);
  }

  public ActionChain(Action a, FunctionModifier f, int s) {
    addAction(a);
    addFunctionModifier(f, s);
  }

  public void addAction(Action a) {
    if(actions == null) {
      actions = new ArrayList<Action>();
    }
    actions.add(a);
  }

  public void addFunctionModifier(FunctionModifier f, int s) {
    functionModifier = f;
    step = s;
  }

  public int start(int c) {
    int modification = functionModifier.getModification(step);
    ActionChain actionChain = functionModifier.getActionChain(step);
    switch(modification) {
      case (FunctionModifier.ADD_ACTION_CHAIN_BEFORE_STEP):
        c = actionChain.start(c);
      case (FunctionModifier.UNCHANGED_STEP):
      case (FunctionModifier.ADD_ACTION_CHAIN_AFTER_STEP):
        for(Action action : actions) {
          c = action.start(c);
        }
        if(modification == FunctionModifier.ADD_ACTION_CHAIN_AFTER_STEP) {
          c = actionChain.start(c);
        }
        break;
      case (FunctionModifier.REPLACE_STEP_BY_ACTION_CHAIN):
        c = actionChain.start(c);
        break;
      case (FunctionModifier.OVERRIDE_WITH_FAILURE_STEP):
        c = Effect.EFFECT_FAILURE;
        break;
      case (FunctionModifier.OVERRIDE_WITH_SUCCESS_STEP):
        c = Effect.EFFECT_SUCCESS;
        break;
    }
    return c;
  }
}
